<?php

namespace app\home\controller;

use cmf\controller\HomeBaseController;

use app\home\model\UserModel;

use app\home\validate\PersonValidate;

use think\Db;

use think\_COOKIE;

class LoginController extends HomeBaseController

{



	// private



	/*

	登录

	*/

		public function login(){


				if (empty( $_COOKIE ['username'] ) || empty( $_COOKIE ['password'] ))   

        			{   
        				$username = '';
        				$password = '';
       					 $this->assign('username',$username);
       					 $this->assign('password',$password);
       				}else{

       					$username = $_COOKIE['username'];
       					$password = $_COOKIE['password'];
       					session('username',$username);

       					if ($username) {
       					$this->assign('username',$username);
       					$this->assign('password',$password);
       					}
       					 
        			} 

					return $this->fetch();
       			}

				





		public function chake(){

			$data = input('post.');
			if ($data['username']) {
				
			
			if ($data['username'] == 'fudaoyuan') {
				
			

			$user = new UserModel();

			$result = $user->where('user_login',$data['username'])->find();

			

				if ($result) {

					if ($data['password']) {
						
					
					if ($result['user_pass'] == cmf_password($data['password'])) {

					session('username',$result['user_login']);

					session('usrid',$result['id']);



					if ($data['remember'] == 1) {



					setcookie( "username", $data['username'], time () + 60*60*48);  

    				setcookie( "password", $data['password'], time () + 60*60*48); 



					// if (empty($_COOKIE['username']) && empty($_COOKIE['password'])) {

						

					// }else{

					// 	$username = $_COOKIE['username'];

					// 	$password = $_COOKIE['password'];

					// 	setcookie( "username", $username, time () - 60*60*48);  

    	// 				setcookie( "password", $password, time () - 60*60*48);  

					// }

						}



					$this->success('登录成功');

					}else {

					

					$this->error('密码错误');

					  }
					 }else{
					 $this->error('密码不能为空');
					 }



					}else {

					$this->error('用户名不存在');

				 	  }

				}else{
					$this->error('用户名错误');

				}
			}else{
				$this->error('用户名不能为空');	
			}

			}





// public function chake(){

// 		$data = input('post.');

		

		

// 		$user = new UserModel();

// 		$result = $user->where('username',$data['username'])->find();

		

// 			if ($result) {

// 				if ($result['password'] === $data['password']) {

// 					session('username',$result['username']);

// 					session('usrid',$result['id']);;

// 					$this->success('登录成功',url('home/index/index'));

// 				}else {

					

// 					$this->error('密码错误');

// 				}



// 			}else {

// 				$this->error('用户名不存在');

// 			}

		

// }

}